﻿using System;
using System.Collections.Generic;
using MS.InformationManagement.DAL.DTOs;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Query;
using Microsoft.SharePoint;
using System.Data;
using System.Linq;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint.Linq;

namespace MS.InformationManagement.BLL.Reports
{
    public class CostReportBLL
    {
        public static List<PieChartDTO> GetCostReport(DateTime startDate, DateTime endDate)
        {
            List<PieChartDTO> foundElements = new List<PieChartDTO>();
            //            string searchCriteria = string.Format("AND (Write>='{0:yyyy/mm/dd}') AND (Write<='{1:yyyy/mm/dd}')", startDate, endDate);
            //            SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));
            //            FullTextSqlQuery query = new FullTextSqlQuery(proxy);
            //            string queryFilter = !string.IsNullOrEmpty(searchCriteria) ? string.Format("AND FREETEXT(DefaultProperties, '{0}')", searchCriteria) : string.Empty;
            //            query.QueryText = string.Format(@"SELECT    IMDocumentTipoRadicado, 
            //                                                        IMDocumentUsuarioActual, 
            //                                                        IMItemId,
            //                                                        IMDocumentRemitenteDestinatario FROM Scope() WHERE ( (""SCOPE"" = 'Information Management - Documents') ) {0} ORDER BY ""Rank"" DESC", queryFilter);
            //            query.TrimDuplicates = true;
            //            query.ResultTypes |= ResultType.RelevantResults;
            //            query.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.SharepointSearch;
            //            query.KeywordInclusion = KeywordInclusion.AnyKeyword;

            //            ResultTableCollection results = query.Execute();
            //            ResultTable relevantResults = results[ResultType.RelevantResults];
            //            DataTable dtResults = new DataTable();
            //            dtResults.Load(relevantResults, LoadOption.OverwriteChanges);

            //            foundElements = (from DataRow row in dtResults.Rows
            //                             group row by row.Field<String>(0) into g
            //                             select new PieChartDTO { Label = g.Key, Value = g.Count() }).ToList();i

            using (DCDocumentCenterDataContext ctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                EntityList<MS.InformationManagement.DAL.DocumentCenter.Radicado> entityList = ctx.GetList<MS.InformationManagement.DAL.DocumentCenter.Radicado>("administrador");
                foundElements = (from radicado in entityList
                                 group radicado by radicado.TipoRadicado into g
                                 select new PieChartDTO { Label = string.IsNullOrEmpty(g.Key) ? "Prueba" : g.Key, Value = g.Count() }).ToList();
            }

            return foundElements;
        }
    }
}